package com.zhanglu.chatbot.weixin;

import com.zhanglu.chatbot.bean.request.WeixinMessageRequest;
import com.zhanglu.chatbot.bean.request.WeixinServerRequest;
import com.zhanglu.chatbot.util.JsonTools;
import com.zhanglu.chatbot.util.WeixinUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author zhanglu
 * @description
 * @date 2017/11/10 20:03
 */
@Slf4j
@RestController
public class WeixinRouter {
    @Autowired
    private WeixinUtil    weixinUtil;
    @Autowired
    private WeixinService weixinService;

    @GetMapping("/weixin/message")
    public String getWeixinServerMessage(WeixinServerRequest weixinServerRequest) {
        log.info("getWeixinServerMessage receive param:[json:{}]", JsonTools.showJSONStringFormat(weixinServerRequest));
        return weixinUtil.checkSignature(weixinServerRequest.getSignature(), weixinServerRequest.getTimestamp(), weixinServerRequest.getNonce()) ? weixinServerRequest.getEchostr() : null;
    }

    @PostMapping(value = "/weixin/message", consumes = "text/xml;charset=utf-8")
    public String getWeixinClientMessage(WeixinMessageRequest message) {
        log.info("getWeixinClientMessage receive param:[json:{}]", JsonTools.showJSONStringFormat(message));
        return weixinService.getWeixinClientMessage(message);
    }
}
